package com.vip.shopbase.Service.impl;

import com.vip.shopbase.Dao.GenericDao;
import com.vip.shopbase.Domain.BaseEntity;
import com.vip.shopbase.Service.GenericManager;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;

@Transactional
public class GenericManagerImpl<T extends BaseEntity<PK>, PK extends Serializable> implements GenericManager<T, PK> {

    protected GenericDao<T, PK> dao;

    @Override
    public void delete(PK id) {
        this.dao.deleteById(id);
    }

    @Override
    public List<T> findAll() {
        return  this.dao.findAll();
    }

    @Override
    public Page<T> findAll(Pageable page) {
        Page<T> result = this.dao.findAll(page);
        return result;
    }

    @Override
    public Page<T> findByName(String name, Pageable pageable) {
        return this.dao.findByNameLike("%"+name+"%",pageable);
    }

    @Override
    public T findById(PK id) {
        return this.dao.findById(id).get();
    }

    @Override
    public Iterable<T> save(Iterable<T> entities) {
        return this.dao.saveAll(entities);
    }

    @Override
    public T save(T entity) {
        T data = this.dao.save(entity);
        return data;
    }

    @Override
    public T saveAndFlush(T entity) {
        return this.dao.saveAndFlush(entity);
    }

    @Override
    public void deleteAll(PK[] ids ) {
        for (PK id : ids) {
            this.dao.deleteById(id);
        }
    }



}
